<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Admin extends \app\BaseModel
{
    protected $name = "shequshop_school_admin";
    public function getPhoneAttr($value, $data)
    {
        if(!empty($value) && isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
            return substr_replace($value, "****", 2, 4);
        }
        return $value;
    }
    public function getLoginAuthPhoneAttr($value, $data)
    {
        if(isset($value) && isset($data["phone"])) {
            if(empty($value)) {
                $value = $data["phone"];
            }
            if(isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
                return substr_replace($value, "****", 2, 4);
            }
            return $value;
        }
    }
    public function getAdminPidAttr($value, $data)
    {
        if(!empty($value)) {
            $admin_model = new Admin();
            $admin = $admin_model->where(["id" => $value, "status" => 1, "sub_agent_auth" => 1])->count();
            return 0 < $admin ? $value : 0;
        }
        return 0;
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page = 10, $where = [])
    {
        $data = $this->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis, $field = "*")
    {
        $data = $this->where($dis)->field($field)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function adminUserList($dis, $page)
    {
        $data = $this->alias("a")->join("massage_service_user_list b", "a.user_id = b.id", "left")->where($dis)->field("a.*,b.nickName")->group("a.id")->order("a.id")->paginate($page)->toArray();
        return $data;
    }
    public function initAgentName()
    {
        $dis = ["agent_name" => ""];
        $data = $this->where($dis)->select()->toArray();
        if(!empty($data)) {
            foreach ($data as $v) {
                if(isset($v["agent_name"])) {
                    $this->dataUpdate(["id" => $v["id"]], ["agent_name" => $v["username"]]);
                }
            }
        }
        return true;
    }
    public function jionAdminCheck($input)
    {
        if($input["username"] && $input["username"] == "admin") {
            return ["code" => 500, "msg" => "用户名不能和超管账号相同"];
        }
        $dis[] = ["user_id", "=", $input["user_id"]];
        $dis[] = ["is_admin", "=", 0];
        $dis[] = ["uniacid", "=", $input["uniacid"]];
        $dis[] = ["status", ">", -1];
        if(!empty($input["id"])) {
            $dis[] = ["id", "<>", $input["id"]];
        }
        $find = $this->where($dis)->find();
        if(!empty($find)) {
            return ["code" => 500, "msg" => "该用户已经绑定过加盟商"];
        }
        $where[] = ["username", "=", $input["username"]];
        $where[] = ["uniacid", "=", $input["uniacid"]];
        $where[] = ["status", ">", -1];
        if(!empty($input["id"])) {
            $where[] = ["id", "<>", $input["id"]];
        }
        $find = $this->where($where)->find();
        if(!empty($find)) {
            return ["code" => 500, "msg" => "已经有该用户名的账户"];
        }
        return true;
    }
    public function getAdminAndSon($admin_id)
    {
        $dis = ["admin_pid" => $admin_id];
        $id = $this->where($dis)->column("id");
        $id[] = $admin_id;
        return $id;
    }
    public function agentBalanceData($admin_id, $order)
    {
        $admin = $this->dataInfo(["id" => $admin_id, "status" => 1, "agent_coach_auth" => 1]);
        $order["admin_balance"] = !empty($admin) ? $admin["balance"] : 0;
        $order["admin_id"] = !empty($admin) ? $admin["id"] : 0;
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $order["uniacid"], ["caradmin"]);
        if($auth["caradmin"]) {
            $order["caradmin"] = !empty($admin) ? $admin["car_admin"] : 0;
        } else {
            $order["caradmin"] = 0;
        }
        if(!empty($admin) && $admin["city_type"] == 1) {
            $order["city_admin"] = "surplus_cash";
            $order["city_admin_id"] = "admin_id";
        }
        if(!empty($admin) && !empty($admin["admin_pid"])) {
            $order["level_balance"] = $admin["level_balance"];
            $order["admin_pid"] = 0 < $admin["level_balance"] ? $admin["admin_pid"] : 0;
            $admin_pdata = $this->dataInfo(["id" => $order["admin_pid"], "status" => 1]);
            if(!empty($admin_pdata) && in_array($admin_pdata["city_type"], [1, 3])) {
                $order["p_level_balance"] = $admin_pdata["level_balance"];
                $order["p_admin_pid"] = 0 < $admin_pdata["level_balance"] ? $admin_pdata["admin_pid"] : 0;
                $order["city_admin"] = "level_cash";
                $order["city_admin_id"] = "admin_pid";
            }
        }
        if($order["free_fare"] == 3 && $order["admin_id"] == 0) {
            $order["free_fare"] = 1;
        }
        return $order;
    }
    public function agentCashData($order, $admin_cash)
    {
        if(isset($order["p_level_balance"])) {
            $order["p_level_cash"] = round($order["p_level_balance"] * $order["true_service_price"] / 100, 2);
            $order["p_level_cash"] = 0 < $order["p_level_cash"] - $admin_cash ? $admin_cash : $order["p_level_cash"];
            $admin_cash -= $order["p_level_cash"];
        }
        if(isset($order["level_balance"])) {
            $order["level_cash"] = round($order["level_balance"] * $order["true_service_price"] / 100, 2);
            $order["level_cash"] = 0 < $order["level_cash"] - $admin_cash ? $admin_cash : $order["level_cash"];
            $admin_cash -= $order["level_cash"];
        }
        $order["over_cash"] = $admin_cash;
        return $order;
    }
    public function agentBalanceDataCustom($admin_id, $order)
    {
        $admin = $this->dataInfo(["id" => $admin_id]);
        $order["admin_id"] = $admin_id;
        if(!empty($admin)) {
            $distri_model = new DistributionConfig();
            $name = $admin["city_type"] == 1 ? "getCityCash" : "getDistrictCash";
            $config = $distri_model->dataInfo(["uniacid" => $order["uniacid"], "name" => $name]);
            $order[$config["balance_name"]] = $config["balance"];
            $order["admin_balance_name"] = $config["balance_name"];
            $order["admin_cash_name"] = $admin["city_type"] == 1 ? "city_cash" : "district_cash";
            $admin_pdata = $this->dataInfo(["id" => $admin["admin_pid"]]);
            if(!empty($admin_pdata) && $admin_pdata["city_type"] == 1 && $admin["city_type"] == 2) {
                $config = $distri_model->dataInfo(["uniacid" => $order["uniacid"], "name" => "getCityCash"]);
                $order[$config["balance_name"]] = $config["balance"];
                $order["admin_pid"] = $admin["admin_pid"];
                $order["level_balance_name"] = $config["balance_name"];
                $order["level_cash_name"] = "city_cash";
            }
        }
        return $order;
    }
    public function getAdminId($user)
    {
        if($user["is_admin"] == 3) {
            $user = $this->dataInfo(["id" => $user["admin_id"]]);
        }
        if(empty($user) || !in_array($user["is_admin"], [0])) {
            return [];
        }
        if(isset($user["city_type"]) && $user["city_type"] == 2) {
            return [$user["id"]];
        }
        if(isset($user["city_type"]) && $user["city_type"] == 1) {
            $admin_model = new Admin();
            $id = $admin_model->where(["admin_pid" => $user["id"]])->column("id");
            $id[] = $user["id"];
            return $id;
        }
        if(isset($user["city_type"]) && $user["city_type"] == 3) {
            $admin_model = new Admin();
            $id = $admin_model->where(["admin_pid" => $user["id"]])->column("id");
            $son_id = $admin_model->where("admin_pid", "in", $id)->column("id");
            $id = array_merge($id, $son_id);
            $id[] = $user["id"];
            return $id;
        }
        return [];
    }
    public function checkAuthData($data)
    {
        $admin_where = ["user_id" => $data["id"], "status" => 1];
        $admin_user = $this->dataInfo($admin_where);
        $arr["is_admin"] = !empty($admin_user) ? 1 : 0;
        $arr["admin_id"] = !empty($admin_user) ? $admin_user["id"] : 0;
        $arr["agent_name"] = !empty($admin_user) ? $admin_user["agent_name"] : "";
        $arr["wallet_status"] = in_array($arr["is_admin"], [1]) ? 1 : 0;
        return $arr;
    }
    public function checkDatas($uniacid)
    {
        $p = new \app\node\info\PermissionNode($uniacid);
        $auth_num = $p->getAuthNumber();
        $num = $this->where(["uniacid" => $uniacid, "is_admin" => 2])->where("status", ">", -1)->count();
        if($auth_num <= $num) {
            return ["code" => 500, "msg" => "授权数量不足" . $auth_num . "-" . $num];
        }
        return true;
    }
    public function checkAdminCash($admin_id)
    {
        $comm_model = new Commission();
        $wallet_model = new Wallet();
        $cash = $comm_model->where(["status" => 2, "admin_id" => $admin_id])->where("type", "in", [2, 5, 6, 13])->sum("cash");
        $wallet = $wallet_model->where(["user_id" => $admin_id])->where("type", "=", 3)->where("status", "in", [1, 2, 4, 5])->sum("total_price");
        return $cash - $wallet;
    }
    public function agentDataTop($dis, $limit, $start_time, $end_time)
    {
        $data = $this->alias("a")->join("massage_service_order_list b", "a.id = b.admin_id AND b.pay_time>0 AND b.create_time between " . $start_time . " AND " . $end_time, "left")->where($dis)->field("a.agent_name,ifnull(round(sum(if(b.coach_refund_time>0,0,b.true_service_price)),2),0) as sale_price,a.id")->group("a.id")->order("sale_price desc,a.id desc")->paginate($limit)->toArray();
        return $data;
    }
}

?>